# -*- coding: utf-8 -*-
import json
import requests
from urllib.parse import urljoin
import time
import random

BASE_URL = 'https://health.tripaway.cn'
AUTH = ('admin', 'admin')
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0',
    'Host': 'health.tripaway.cn',
    'Origin': 'https://health.tripaway.cn'
}


def user_login():
    payload = {'username': 'health_李沐夏@&L858717@&@&@&@&'}
    rsp = requests.post(
        urljoin(BASE_URL, '/login'),
        data=payload,
        headers=HEADERS,
        allow_redirects=False)
    return rsp


def upload_temp(cks):
    randint_data = random.randint(1, 7)
    json_data = dict(
        remark='健康，无以下状况,',
        temp='36.' + str(randint_data),
        recordTimeStr=time.strftime("%Y-%m-%d %H:%M", time.localtime()),
        field1='腋温'
    )
    rsp = requests.post(urljoin(BASE_URL, '/tempRecord/add'),
                        headers={
                            'Content-Type': 'application/json;'
                        },
                        cookies=cks,
                        json=json_data)
    return rsp


r = user_login()
cookies = requests.utils.dict_from_cookiejar(r.cookies)
te = upload_temp(cookies)
print(te.status_code)
print(te.content)
